package com.example.a33mvvm.fragment_viewpage;

import android.content.Intent;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.PagerSnapHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;

import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.example.a33mvvm.R;
import com.example.a33mvvm.adapter.CommentAdapter;
import com.example.a33mvvm.adapter.MultiItem;
import com.example.a33mvvm.databinding.FragmentVideoBinding;
import com.example.a33mvvm.live.LiveActivity;
import com.example.a33mvvm.video.VideoViewModel;
import com.example.base_mvvm.base.view.BaseFragment;
import com.example.base_mvvm.entity.CommentEntity;
import com.example.base_mvvm.entity.FollowEntity;
import com.example.base_mvvm.entity.LikeEntity;
import com.example.base_mvvm.entity.VideoEntity;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.media.UMImage;
import com.umeng.socialize.media.UMWeb;

import java.util.ArrayList;
import java.util.List;


public class VideoFragment extends BaseFragment<FragmentVideoBinding, VideoViewModel> {
    private MultiItem videoAdapter;
    private List<VideoEntity.DataBean> videos = new ArrayList<>();
    private int current_position = 0;
    private StandardGSYVideoPlayer player;
    private List<CommentEntity.DataBean> comments = new ArrayList<>();
    private CommentAdapter commentAdapter;
    @Override
    protected void initData() {
        viewModel.video(6,10);
        viewModel.getComment_success.observe(this, new Observer<CommentEntity>() {
            @Override
            public void onChanged(CommentEntity commentEntity) {
                if(commentEntity.getCode() == 200){
                    comments.clear();
                    comments.addAll(commentEntity.getData());
                    commentAdapter.notifyDataSetChanged();
                }
            }
        });
        viewModel.getComment_failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
        //发送评论成功
        viewModel.putComment_success.observe(this, commentEntity -> {
            if(commentEntity.getCode() == 200){
                commentAdapter.getData().addAll(0,commentEntity.getData());
                commentAdapter.notifyDataSetChanged();
                RecyclerView  view = (RecyclerView) commentAdapter.getViewByPosition(current_position, R.id.item_video_comment_rv);
                if(view != null){
                    view.scrollToPosition(0);
                }
            }else {
                ToastUtils.showLong(commentEntity.getMessage());
            }
        });
        viewModel.putComment_failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
        viewModel.video_success.observe(this, new Observer<VideoEntity>() {
            @Override
            public void onChanged(VideoEntity videoEntity) {
                videos.clear();
                if(videoEntity.getCode() == 200){
//                    videos.addAll(videoEntity.getData());
                    videoAdapter.getData().addAll(videoEntity.getData());
                    videoAdapter.notifyDataSetChanged();
                }
            }
        });
        viewModel.video_failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Log.d("sss", "onChanged: "+s);
            }
        });
        viewModel.disLike_success.observe(this, new Observer<LikeEntity>() {
            @Override
            public void onChanged(LikeEntity likeEntity) {
                if(likeEntity.getCode() == 200){
                    videoAdapter.getData().get(current_position).setDianzan(0);
//                    videos.get(current_position).setDianzan(0);
                    videoAdapter.notifyItemChanged(current_position);
                    ToastUtils.showLong(likeEntity.getMessage());
                }
            }
        });
        viewModel.disLike_failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
        viewModel.like_success.observe(this, new Observer<LikeEntity>() {
            @Override
            public void onChanged(LikeEntity likeEntity) {
                if(likeEntity.getCode() == 200){
                    videoAdapter.getData().get(current_position).setDianzan(1);
//                    videos.get(current_position).setDianzan(1);
                    videoAdapter.notifyItemChanged(current_position);
                    ToastUtils.showLong(likeEntity.getMessage());
                }
            }
        });
        viewModel.like_failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
        viewModel.follow_success.observe(this, new Observer<FollowEntity>() {
            @Override
            public void onChanged(FollowEntity followEntity) {
                if(followEntity.getCode() == 200){
                    videoAdapter.getData().get(current_position).setGuanzhu(1);
//                    videos.get(current_position).setGuanzhu(1);
                    videoAdapter.notifyItemChanged(current_position);
                    ToastUtils.showLong(followEntity.getMessage());
                }
            }
        });
        viewModel.follow_failure.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                ToastUtils.showLong(s);
            }
        });
    }

    @Override
    protected void initView() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
        dataBinding.videoRec.setLayoutManager(layoutManager);
        PagerSnapHelper pagerSnapHelper = new PagerSnapHelper();
        pagerSnapHelper.attachToRecyclerView(dataBinding.videoRec);
        videoAdapter = new MultiItem();
        videoAdapter.getData().addAll(videos);//直接通过adapter的data给数据源赋值
        dataBinding.videoRec.setAdapter(videoAdapter);
        dataBinding.videoRec.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View view, int i, int i1, int i2, int i3) {
                current_position = layoutManager.findFirstVisibleItemPosition();
                player = (StandardGSYVideoPlayer) videoAdapter.getViewByPosition(current_position, R.id.live_gsy_player);
                if(player != null) {
                    player.startPlayLogic();
                }
            }
        });
        videoAdapter.addChildClickViewIds(R.id.item_video_add_iv,R.id.item_video_like_iv,R.id.item_video_comment_iv,R.id.item_video_share_iv,
                R.id.item_video_close,R.id.item_video_send_iv,R.id.item_video_live_iv,R.id.video_toLive);
        videoAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                VideoEntity.DataBean dataBean = (VideoEntity.DataBean) adapter.getData().get(position);
//                VideoEntity.DataBean dataBean = videos.get(position);
                switch (view.getId()){
                    case R.id.item_video_add_iv:
                        follow(dataBean);
                        break;
                    case R.id.item_video_like_iv:
                        like(dataBean);
                        break;
                    case R.id.item_video_comment_iv:
                        comment(dataBean);
                        break;
                    case R.id.item_video_share_iv:
                        share(dataBean);
                        break;
                    case R.id.item_video_close:
                        close(dataBean);
                        break;
                    case R.id.item_video_send_iv:
                        send(dataBean);
                        break;
                    case R.id.item_video_live_iv:
                        full(dataBean);
                        break;
                    case R.id.video_toLive:
                        toLive(dataBean);
                        break;
                }
            }
        });
    }

    private void toLive(VideoEntity.DataBean dataBean) {
        Intent intent = new Intent(requireContext(), LiveActivity.class);
        intent.putExtra("dataBean",dataBean);
        startActivity(intent);
    }

    private void full(VideoEntity.DataBean dataBean) {
        StandardGSYVideoPlayer player1 = (StandardGSYVideoPlayer) videoAdapter.getViewByPosition(current_position, R.id.live_gsy_player);
        assert player1 != null;
           player1.setRotateViewAuto(true);
            player1.startWindowFullscreen(requireContext(),false,true);


    }

    private void send(VideoEntity.DataBean dataBean) {
        EditText sendMsg = (EditText) videoAdapter.getViewByPosition(current_position, R.id.item_video_input_et);
        String msg = sendMsg.getText().toString().trim();
        if(TextUtils.isEmpty(msg)) {
            ToastUtils.showLong("输入框不能为空,请文明用语");
            return;
        }
        viewModel.putComment(msg,dataBean.getId());
        sendMsg.setText("");
    }
    private void close(VideoEntity.DataBean dataBean) {
        dataBinding.videoRec.setIntercept(false);
        View layout = videoAdapter.getViewByPosition(current_position, R.id.item_video_comment_ll);
        layout.setVisibility(View.VISIBLE);
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_out);
        animation.setDuration(1000);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                layout.setVisibility(View.GONE);
                dataBinding.videoRec.setIntercept(true);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        layout.startAnimation(animation);
    }

    private void share(VideoEntity.DataBean dataBean) {

        UMImage umImage = new UMImage(requireContext(),dataBean.getVideomainimg());
        umImage.compressStyle= UMImage.CompressStyle.SCALE;
        UMWeb umWeb = new UMWeb(dataBean.getVideopath());
        umWeb.setDescription("我是闪避");
        umWeb.setThumb(umImage);
        umWeb.setTitle("闪避");
        new ShareAction(requireActivity())
                .withMedia(umWeb)
                .open();

    }

    private void comment(VideoEntity.DataBean dataBean) {
        dataBinding.videoRec.setIntercept(false);
        View layout = videoAdapter.getViewByPosition(current_position, R.id.item_video_comment_ll);
        layout.setVisibility(View.VISIBLE);
        Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.anim_in);
        animation.setDuration(1000);
        animation.setInterpolator(new AccelerateInterpolator());
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                viewModel.getComment(dataBean.getId());
            }

            @Override
            public void onAnimationEnd(Animation animation) {

            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        layout.startAnimation(animation);
        RecyclerView comment = (RecyclerView) videoAdapter.getViewByPosition(current_position, R.id.item_video_comment_rv);
        comment.setLayoutManager(new LinearLayoutManager(getContext()));
        commentAdapter = new CommentAdapter(comments);
        comment.setAdapter(commentAdapter);
    }

    private void like(VideoEntity.DataBean dataBean) {
        if(dataBean.getDianzan() == 0){
            viewModel.like(dataBean.getId());
        }else {
            viewModel.disLike(dataBean.getId());
        }
    }

    private void follow(VideoEntity.DataBean dataBean) {
        if(dataBean.getGuanzhu() == 0){
            viewModel.follow(dataBean.getAuthname());
        }
    }

    @Override
    protected Class<VideoViewModel> getViewModel() {
        return VideoViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_video;
    }
}